package tgc.edu.ljc.bookmanage.web.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.ljc.bookmanage.custom.CommonController;
import tgc.edu.ljc.bookmanage.custom.DataGridParam;
import tgc.edu.ljc.bookmanage.custom.EasyuiResult;
import tgc.edu.ljc.bookmanage.custom.EasyuiUtils;
import tgc.edu.ljc.bookmanage.entity.TbClass;
import tgc.edu.ljc.bookmanage.service.ClassService;
import tgc.edu.ljc.bookmanage.service.TbDeptService;
import tgc.edu.ljc.bookmanage.web.form.ClassForm;

@Controller
@RequestMapping(value="/banji")
public class ClassController extends CommonController<TbClass, Integer,ClassForm>{
	@Autowired
	private ClassService service;
	@Autowired
	private TbDeptService deptService;
	
	@RequestMapping(value="/manage")
	public void manage() {
		
	}
	
	public Object page(DataGridParam param,ClassForm form) {
		Pageable pageable = param.buildPageable();
		Specification<TbClass> spec = builidSpec(form);
		Page<TbClass> page = service.findAll(spec,pageable);
		return EasyuiUtils.buildDataGridResult(page);
	}
	
	private Specification<TbClass> builidSpec(ClassForm form) {
		Specification<TbClass> spec=new Specification<TbClass>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<TbClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String name = form.getName();
				if(StringUtils.hasText(name)) {
					rules.add(cb.like(root.get("name"), "%"+name+"%"));
				}
				String manager = form.getManager();
				if(StringUtils.hasText(manager)) {
					rules.add(cb.like(root.get("manager"), "%"+manager+"%"));
				}
				
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	public void edit(Integer id, ModelMap map) {
		TbClass model = new TbClass();
		if(id!=null) {
			model=service.findById(id);
		}
		List<HashMap<String, Object>> deptData = deptService.combotree(null, null);
		map.put("model", model);
		map.put("deptData", deptData);
	}
	
	public Object save(ClassForm form) {
		TbClass model=new TbClass();
		Integer id = form.getId();
		if(id!=null) {
			model=service.findById(id);
		}
		BeanUtils.copyProperties(form, model,"id");
		service.save(model);
		return new EasyuiResult("数据保存成功");
	}
	
	public Object delete(Integer id) {
		try {
			service.deleteById(id);
			return new EasyuiResult("数据删除成功");
		} catch (Exception e) {
			return new EasyuiResult(false, "数据删除失败");
		}
	}
	
	public void search(ClassForm form,ModelMap map) {
		map.put("model", form);
	}
}
